home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 002 / make / execute.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  6KB  |  313 lines

  1. #ifndef AMIGA
  2. #  include <errno.h>
  3. #endif
  4.  
  5. #ifdef MSDOS
  6. #  include <bdos.h>
  7. #endif
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include "make.h"
  12.  
  13. #ifndef FALSE
  14. #  define FALSE (0)
  15. #endif
  16.  
  17. #ifndef TRUE
  18. #  define TRUE (1)
  19. #endif
  20.  
  21. #define BUFFSIZ (256)
  22.  
  23. #ifdef MSDOS
  24. extern char *searchpath ();
  25. #endif
  26.  
  27. extern char *getenv ();
  28.  
  29. #ifdef MSDOS
  30. extern int _envseg;
  31. #ifndef NOREALEXECUTE
  32. static char param[256];
  33. static char cmd[256];
  34. static char *cmds[] = {
  35.     "break",
  36.     "chdir",
  37.     "cd",
  38.     "cls",
  39.     "copy",
  40.     "ctty",
  41.     "date",
  42.     "del",
  43.     "erase",
  44.     "dir",
  45.     "echo",
  46.     "exit",
  47.     "for",
  48.     "goto",
  49.     "if",
  50.     "mkdir",
  51.     "md",
  52.     "path",
  53.     "pause",
  54.     "prompt",
  55.     "rem",
  56.     "ren",
  57.     "rename",
  58.     "rmdir",
  59.     "rd",
  60.     "set",
  61.     "shift",
  62.     "time",
  63.     "type",
  64.     "ver",
  65.     "verify",
  66.     "vol",
  67.     0
  68. };
  69. #endif    /* !NOREALEXECUTE */
  70. #endif    /* MSDOS */
  71.  
  72. execute (str, noexecflag)
  73. char *str;
  74. int noexecflag;
  75. {
  76.     auto char tmp[BUFFSIZ];
  77.     auto char buf[10];
  78.     register int index = 0;
  79.     register int rval;
  80.     extern int ignore_errors;
  81.  
  82.     DBUG_ENTER ("execute");
  83.     tmp[0] = EOS;
  84.     while (*str != EOS) {
  85.     if (*str == '\n') {
  86.         tmp[index] = EOS;
  87.         index = 0;
  88.         str++;
  89.         if ((rval = run (tmp, noexecflag)) != 0 && !ignore_errors) {
  90.         fputs ("***Error Code ", stderr);
  91.         itoa (rval, buf);
  92.         fputs (buf, stderr);
  93.         fputc ('\n', stderr);
  94.         DBUG_RETURN (rval);
  95.         }
  96.     } else if (index == (BUFFSIZ - 1)) {
  97.         fputs ("Command Too Long: ", stderr);
  98.         fputs (str, stderr);
  99.         fputs ("\nShorten.\n", stderr);
  100.         DBUG_RETURN (-1);
  101.     } else {
  102.         tmp[index++] = *str++;
  103.     }
  104.     }
  105.     DBUG_RETURN (0);
  106. }
  107.  
  108.  
  109.  
  110. #ifdef TESTING
  111. main ()
  112. {
  113.     auto char temp[128];
  114.  
  115.     for (;;) {
  116.     printf ("Command: ");
  117.     gets (temp);
  118.     if (temp[0] == EOS) {
  119.         break;
  120.     }
  121.     printf ("        Execute: %d\n", run (temp));
  122.     }
  123. }
  124. #endif
  125.  
  126. /* run(str)
  127.  * char *str;
  128.  *        returns the value of the executed command.  If the command is
  129.  *        an MS-DOS resident command the command.com is executed else
  130.  *        the program is invoked (looking down the PATH).
  131.  *
  132.  *        Written: Bradley N. Davis  University of Utah VCIS group
  133.  *        Date: 4-Jan-84
  134.  *
  135.  */
  136.  
  137. static int run (str, noexecflag)
  138. char *str;
  139. int noexecflag;
  140. {
  141. #ifdef MSDOS
  142.     auto struct execp ep;
  143.     auto struct SREGS segs;
  144. #else
  145.     extern int system ();
  146. #endif
  147.     auto int status = 0;
  148.  
  149.     DBUG_ENTER ("run");
  150.     while (*str == '\t' || *str == ' ') {
  151.     str++;
  152.     }
  153.     putchar ('\t');
  154.     puts (str);
  155.     fflush (stdout);
  156.     if (!noexecflag) {
  157. #ifndef NOREALEXECUTE
  158. #ifdef MSDOS
  159.     if (str[0] == EOS) {    /* Blank Line? push to subshell */
  160.         strcpy (cmd, getenv ("COMSPEC"));
  161.         param[0] = EOS;
  162.         segread (&segs);
  163.         ep.ex_envseg = _envseg;
  164.         ep.ex_cmdadd = (unsigned) param;
  165.         ep.ex_cmdseg = segs.ss;
  166.         ep.ex_fcb1ad = 0;
  167.         ep.ex_fcb1sg = 0;
  168.         ep.ex_fcb2ad = 0;
  169.         ep.ex_fcb2sg = 0;
  170.         status = (exec (cmd, 0, &ep));
  171.     } else if (resident (str)) {
  172.         status = (system (str));
  173.     } else {
  174.         status = (program (cmd, param));
  175.     }
  176. #else
  177.     CHECK_ABORT;
  178.     status = system (str);
  179. #endif    /* MSDOS */
  180. #endif  /* !NOREALEXECUTE */
  181.     }
  182.     DBUG_3 ("sys", "subcommand returns status %d", status);
  183.     DBUG_RETURN (status);
  184. }
  185.  
  186. /*
  187.  * resident(str)
  188.  * char *str;
  189.  *        returns true if the command in str is an MS-DOS resident
  190.  *        command.
  191.  *
  192.  *        Written: Bradley N. Davis  University of Utah VCIS group
  193.  *        Date: 4-Jan-84
  194.  *
  195.  */
  196.  
  197. #define iswhite(ch) (ch == ' ' || ch == '\t')
  198.  
  199. #ifdef MSDOS
  200. #ifndef NOREALEXECUTE
  201. static int resident (str)
  202. char *str;
  203. {
  204.     register char **t;
  205.     extern char *strpbrk ();
  206.     register int i;
  207.     register int j;
  208.  
  209.     DBUG_ENTER ("resident");
  210.     while (iswhite (*str)) {
  211.     str++;            /* trim blanks */
  212.     }
  213.     if (str[1] == ':' && isalpha (str[0])) {    /* look for x: */
  214.     DBUG_RETURN (TRUE);
  215.     }
  216.     if (strpbrk (str, "<>|") != NULL) {        /* redirection? use system */
  217.     DBUG_RETURN (TRUE);
  218.     }
  219.     i = 0;
  220.     while (isalnum (*str)) {
  221.     if (isupper (*str)) {
  222.         cmd[i++] = *str++ - 'A' + 'a';
  223.     } else {
  224.         cmd[i++] = *str++;
  225.     }
  226.     }
  227.     cmd[i] = EOS;
  228.     for (t = cmds; *t; t++) {
  229.     if (strcmp (*t, cmd) == 0) {
  230.         DBUG_RETURN (TRUE);
  231.     }
  232.     }
  233.     strcat (cmd, ".bat");    /* Batch file? use system */
  234.     if (searchpath (cmd) != 0) {
  235.     cmd[i] = EOS;
  236.     DBUG_RETURN (TRUE);
  237.     }
  238.     cmd[i] = EOS;
  239.     j = strlen (str);
  240.     i = 1;
  241.     while ((param[i++] = *str++) != 0);
  242.     param[0] = j;
  243.     param[j + 1] = '\r';
  244.     DBUG_RETURN (FALSE);
  245. }
  246.  
  247. static int program (pcmd, pparam)
  248. char *pcmd;
  249. char *pparam;
  250. {
  251. #ifdef MSDOS
  252.     auto struct execp ep;
  253.     auto struct SREGS segs;
  254.     register char *pathp;
  255.     register int len;
  256.  
  257.     DBUG_ENTER ("program");
  258.     len = strlen (pcmd);
  259.     strcat (pcmd, ".com");
  260.     pathp = searchpath (pcmd);
  261.     if (pathp == 0) {
  262.     pcmd[len] = EOS;
  263.     strcat (pcmd, ".exe");
  264.     pathp = searchpath (pcmd);
  265.     if (pathp == 0) {
  266.         pcmd[len] = EOS;
  267.         errno = ENOENT;
  268.         DBUG_RETURN (-1);
  269.     }
  270.     }
  271.     segread (&segs);
  272.     ep.ex_envseg = _envseg;
  273.     ep.ex_cmdadd = (unsigned) pparam;
  274.     ep.ex_cmdseg = segs.ss;
  275.     ep.ex_fcb1ad = 0;
  276.     ep.ex_fcb1sg = 0;
  277.     ep.ex_fcb2ad = 0;
  278.     ep.ex_fcb2sg = 0;
  279.     DBUG_RETURN (exec (pathp, 0, &ep));
  280. #else
  281.     fprintf (stderr, "Urk -- 'program()' unimplemented!\n");
  282.     DBUG_RETURN (0);
  283. #endif
  284. }
  285. #endif    /* !NOREALEXECUTE */
  286. #endif    /* MSDOS */
  287.  
  288.  
  289. #ifdef AMIGA
  290. /*
  291.  *    Do explicit check for abort.  When Enable_Abort is non-zero,
  292.  *    Chk_Abort() cause program termination if CNTRL-C or CNTRL-D has
  293.  *    been received.  Thus, we temporarily set it back to zero while we
  294.  *    do the explicit test, so we can do our own clean up and exit.
  295.  *    Note that if the -V flag was used, we spit out a confirming message
  296.  *    that we are quitting.
  297.  *
  298.  */
  299.  
  300. void Check_Abort ()
  301. {
  302.     DBUG_ENTER ("Check_Abort");
  303.     DBUG_2 ("abort", "do explicit test for CNTRL-C");
  304.     DISABLE_ABORT;
  305.     if (Chk_Abort () != 0) {
  306.     exit (1);
  307.     }
  308.     ENABLE_ABORT;
  309.     DBUG_VOID_RETURN;
  310. }
  311.  
  312. #endif    /* AMIGA */
  313.